package com.wuliu.servlet;

import com.wuliu.dao.BusinessClassDAO;
import com.wuliu.dao.impl.BusinessClassDAOImpl;
import com.wuliu.model.BusinessClass;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/business_class/update")
public class AdminBusinessClassUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id=Integer.valueOf(req.getParameter("id"));
        BusinessClassDAO businessClassDAO =new BusinessClassDAOImpl();
        BusinessClass businessClass=businessClassDAO.findById(id);
        req.setAttribute("businessClass",businessClass);
        req.getRequestDispatcher("/WEB-INF/view/admin/business_class_update.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id= Integer.valueOf(req.getParameter("id"));
        String name=req.getParameter("name");
        String desc=req.getParameter("desc");
        String img=req.getParameter("img");
        BusinessClass businessClass=new BusinessClass(id,name,desc,img);
        BusinessClassDAO businessClassDAO=new BusinessClassDAOImpl();
        businessClassDAO.update(businessClass);

        resp.sendRedirect("/admin/business_class");
    }
}
